<?php


namespace Modules\Chinadmin\Services;


use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Validator;
use Mockery\Exception;
use Modules\Chinadmin\Models\AccountModel;
use Modules\Chinadmin\Models\EmployeeModel;
use Modules\Chinadmin\Rules\ValidationRule;

/**
 * 登录
 * Class AuthServices
 * @package Modules\Chinadmin\Services
 */
class AccountServices
{

    /**
     * @var array
     */
    protected $allowFields = ['id','username','nickname','mobile','avatar','expiretime'];

    /**
     * 退出
     *
     * @return bool|\Illuminate\Http\JsonResponse
     */
    public function logout(){
        if(!Auth::guard('dispatch')->check()){
            return response()->json(['code' => 400,'msg' => '还未登录']);
        }
        Auth::guard('dispatch')->logout();
        return true;
    }


    /**
     * 直接登录账号
     * @param int $user_id
     * @return boolean
     */
    public function direct($user_id){

        $user = AccountModel::getUser($user_id);

        if($user){
            DB::beginTransaction();
            try{
                $ip = request()->ip();
                $time = date('Y-m-d H:i:s');

                $user->prevtime = $user->logintime;
                //记录本次登录的IP和时间
                $user->loginip = $ip;
                $user->logintime = $time;
                //重置登录失败次数
                $user->loginfailure = 0;

                //空的才执行更新
                if(empty($user['api_token'])){
                    $user->api_token = uuid();
                }


                EmployeeModel::where("employee_id",$user['employee_id'])->update([
                    "app_status" => "on"
                ]);
                $user->save();

                Db::commit();
            }catch(Exception $e){
                Db::rollback();
                return false;
            }
            return $user;
        }else{
            return false;
        }
    }


    /**
     * 修改密码
     * @param string $newpassword       新密码
     * @param string $oldpassword       旧密码
     * @param bool   $ignoreoldpassword 忽略旧密码
     * @return boolean
     */
    public function changepwd($user_id, $newpassword, $oldpassword = '', $ignoreoldpassword = false)
    {
        try{
            $user = AccountModel::getUser($user_id);
            if($user) {
                if ($user->password == $this->getEncryptPassword($oldpassword) || $ignoreoldpassword) {

                    $newpassword = $this->getEncryptPassword($newpassword);

                    $user->loginfailure = 0;

                    $user->password = $newpassword;
                    return $user->save();
                }else {
                    mdsResponse(400, '旧密码不正确');
                }
            }
            mdsResponse(400, '用户不存在');
        } catch (Exception $e) {
            mdsResponse(400, $e->getMessage()?: '修改失败');
        }
    }



    /**
     * 获取密码加密后的字符串
     * @param string $password 密码
     * @param string $salt 密码盐
     * @return string
     */
    public function getEncryptPassword($password,$salt = '')
    {
        return Hash::make($password . $salt);
    }



}

